package servlets.local;

import hibernate.method.LocalMethods;
import hibernate.model.Local;
import hibernate.util.HibernateUtil;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.google.gson.Gson;

/**
 * Servlet implementation class addLocal
 */
@WebServlet(name = "addLocal", urlPatterns = { "/addLocal" })
public class addLocal extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public addLocal() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		response.setCharacterEncoding("UTF-8");
		List<String> list = new ArrayList<String>();
		List<Local> listLoc = new ArrayList<Local>();
		SessionFactory sessionFactory = HibernateUtil
				.getSessionAnnotationFactory();
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		LocalMethods lm = new LocalMethods();
		listLoc = lm.listAllLocals(sessionFactory, session);

		for (Local loc : listLoc) {
			String LocalData = /* loc.getCountry() +", "+ */loc.getCity()
					+ ", " + loc.getStreet() + " " + loc.getLocal_number();
			list.add(LocalData);
		}
		String json = null;

		json = new Gson().toJson(list);
		response.setContentType("application/json");
		response.getWriter().write(json);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");

		String province = (String) request
				.getParameter("administrative_area_level_1");

		String city = (String) request.getParameter("locality");
		String street = (String) request.getParameter("route");
		String local_number = (String) request.getParameter("street_number");
		String country = (String) request.getParameter("country");
		String description = (String) request.getParameter("description");

		response.setContentType("text/html");
		SessionFactory sessionFactory = HibernateUtil
				.getSessionAnnotationFactory();
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		LocalMethods lm = new LocalMethods();
		lm.addLocal(sessionFactory, session, street, local_number, city,
				province, country, description);

		try {
			session.getTransaction().commit();
			out.println("<script type=\"text/javascript\">alert(\"Lokal dodany\"); window.location.href = \"management/index.jsp?page=main&group=ourLocals\";</script>");
		} catch (Exception e) {
			out.println("<script type=\"text/javascript\">alert(\"Lokal nie zostal dodany\"); window.location.href = \"management/index.jsp?page=main&group=ourLocals\";</script>");
		}

	}
}
